package com.yxhl.zoume.domain.interactor;

import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.MpvModelResponse;
import com.yxhl.zoume.data.http.rest.response.base.BaseResponse;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.domain.event.LoginInvalidEvent;

import java.io.Serializable;

import rx.Subscriber;

/**
 * Subscriber网络请求返回基类
 * 退出登录
 * Created by Xboy on 16/3/23.
 */
public class ZMSubscriber<T> extends Subscriber<T> implements Serializable {

    private static final String KEY_LOGIN_INVALID = "LOGIN_INVALID";

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onNext(T t) {
        String resultCode = null;
        if (t instanceof BaseResponse) {// 登录失效
            BaseResponse baseResponse = (BaseResponse) t;
            resultCode = baseResponse.getResultCode();

        } else if (t instanceof BizOrderResponse) {
            BizOrderResponse bizOrderResponse = (BizOrderResponse) t;
            CommonRespParams commonRespParams = bizOrderResponse.getCommonRespParams();
            if (commonRespParams != null) {
                resultCode = commonRespParams.getResultCode();
            }
        } else if (t instanceof MpvModelResponse) {
            MpvModelResponse mpvModelResponse = (MpvModelResponse) t;
            CommonRespParams commonResponse = mpvModelResponse.getCommonResponse();
            if (commonResponse != null) {
                resultCode = commonResponse.getResultCode();
            }
        }

        if (null != resultCode && resultCode.length() > 0) {
            if (resultCode.equals(KEY_LOGIN_INVALID)) {
                RxBus.getInstance().send(new LoginInvalidEvent());
            }
        }
    }
}
